var express = require('express');
var router = express.Router();
const db = require('../db/db');
const common = require('../lib/common');

router.post('/login', function (req, res, next) {
    let {id, pwd, role, identifyCode, code} = req.body;
    if (identifyCode !== code.toString()) {
        res.send({flag: false, msg: "验证码错误"})
    } else {
        let sql = `select *
                   from teachers
                   where id = ${id}
                     and role = ${role}`;
        db(sql, function (err, result) {
            if (err) {
                res.send({flag: false, msg: "登录失败"});
                throw err;
            } else {
                if (result[0] == null) {
                    res.send({flag: false, msg: "该账号不存在"});
                } else if (result[0].pwd !== common.md5(pwd)) {
                    res.send({flag: false, msg: "密码不正确"});
                } else {
                    req.session.tid = id
                    req.session.role = role
                    req.session.mid = result[0].major_id
                    res.json({flag: true, msg: "登录成功！"})
                }
            }
        })
    }
});

router.get('/logout', function (req, res, next) {
    delete req.session.tid;
    res.json({msg: "退出成功！"})
});

router.post('/add', function (req, res) {
    let {id, name, pwd, role, major} = req.body;
    let sql = `select *
               from teachers
               where id = '${id}'`;
    db(sql, function (err, rows) {
        if (err) {
            throw err;
        } else {
            if (rows.length !== 0) {
                res.send({flag: false, msg: "该账号已经存在"});
            } else {
                sql = `insert into teachers(id,name,pwd,role,major_id) values(${id},'${name}','${common.md5(pwd)}',${role},${major})`
                console.log(sql)
                db(sql, function () {
                    if (err) {
                        res.send({flag: false, msg: "添加失败"});
                        throw err;
                    } else {
                        res.send({flag: true, msg: "添加成功"});
                    }
                })
            }
        }
    })
});

router.post('/update', function (req, res, next) {
    let {pwd} = req.body;
    let id = req.session.tid
    let sql = `update teachers
               set pwd = '${common.md5(pwd)}'
               where id = ${id}`;
    db(sql, function (err, result) {
        if (err) {
            res.send({flag: false, msg: "更新失败\n" + err});
        } else {
            res.send({flag: true, msg: "更新成功"});
        }
    })
});

router.post('/update/:role', function (req, res, next) {
    let role = req.params.role;
    let {id} = req.body
    let sql = `update teachers
               set role = '${role}'
               where id = ${id}`;
    console.log(sql)
    db(sql, function (err, result) {
        if (err) {
            res.send({flag: false, msg: "身份更新失败\n" + err});
        } else {
            res.send({flag: true, msg: "身份更新成功"});
        }
    })
});

router.post('/del', function (req, res) {
    let {id} = req.body
    let sql = "delete from teachers where id=" + id;
    db(sql, function (err, result) {
        if (err) {
            res.send({flag: false, msg: "删除失败\n" + err});
        } else {
            res.send({flag: true, msg: "删除成功"});
        }
    })
})

router.post('/all', function (req, res) {
    let {page, pageSize} = req.body;
    let start = (page - 1) * pageSize;
    let sql = `select teachers.id as id,teachers.name as name,major.name as major,
        role,(select count(*) from teachers) as total
        from teachers,major where major.id=teachers.major_id
        limit ${start},${pageSize}`;
    db(sql, function (err, result) {
        if (err) {
            res.send({flag: false, msg: "获取失败\n" + err});
        } else {
            res.send({flag: true,datas: result, total: result[0]['total']});
        }
    })
})

module.exports = router;